package com.example.myapplication;

import java.util.ArrayList;
import java.util.List;

import android.R.string;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ChatActivity extends Activity {
    private int count = 0;
    private ListView msgListView;

    private EditText inputText;

    private Button send;

    private MsgAdapter adapter;

    private List<Msg> msgList = new ArrayList<>();

    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//设置窗口没有标题栏
        setContentView(R.layout.chat_activity);
        Sinformation sinformation = (Sinformation) getIntent().getSerializableExtra("Sp");
     /*   imageView=(ImageView)findViewById(R.id.iv);
        imageView.setImageBitmap((Bitmap)getIntent().getParcelableExtra("pic"));*/
        adapter = new MsgAdapter(ChatActivity.this, R.layout.msg_item, msgList);
        inputText = (EditText) findViewById(R.id.input_text);
        initMsg(sinformation.getID());
        send = (Button) findViewById(R.id.send);
        msgListView = (ListView) findViewById(R.id.msg_list_view);
        msgListView.setAdapter(adapter);
        msgListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Msg my=msgList.get(position);
                Sinformation sp2 = sinformation;
            /*    if (my.getType()==1)
                {
                    sp2.setID("0");
                    sp2.setRegion("九江");
                    sp2.setName("吴");
                }*/
                Intent intent = new Intent(ChatActivity.this, InformationActivity.class);
                intent.putExtra("Sp", sp2);
                startActivity(intent);
            }
        });
        send.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                String content = inputText.getText().toString();
                if (!"".equals(content) && count % 2 != 0) {
                    Msg msg = new Msg(content, Msg.SENT, R.drawable.person);
                    msgList.add(msg);
                    adapter.notifyDataSetChanged();//有新消息时，刷新ListView中的显示
                    msgListView.setSelection(msgList.size());//将ListView定位到最后一行
                    inputText.setText("");//清空输入框的内容
                    count++;
                } else if (!"".equals(content) && count % 2 == 0) {
                    if (sinformation.getID().equals("1")) {
                        Msg msg = new Msg(content, Msg.RECEIVED, R.drawable.he);
                        msgList.add(msg);
                        adapter.notifyDataSetChanged();//有新消息时，刷新ListView中的显示
                        msgListView.setSelection(msgList.size());//将ListView定位到最后一行
                        inputText.setText("");//清空输入框的内容
                        count++;
                    } else if (sinformation.getID().equals("2")) {
                        Msg msg = new Msg(content, Msg.RECEIVED, R.drawable.lv);
                        msgList.add(msg);
                        adapter.notifyDataSetChanged();//有新消息时，刷新ListView中的显示
                        msgListView.setSelection(msgList.size());//将ListView定位到最后一行
                        inputText.setText("");//清空输入框的内容
                        count++;
                    } else if (sinformation.getID().equals("3")) {
                        Msg msg = new Msg(content, Msg.RECEIVED, R.drawable.cai);
                        msgList.add(msg);
                        adapter.notifyDataSetChanged();//有新消息时，刷新ListView中的显示
                        msgListView.setSelection(msgList.size());//将ListView定位到最后一行
                        inputText.setText("");//清空输入框的内容
                        count++;
                    }
                }
            }

        });
    }

    private void initMsg(String id) {
        if (id.equals("3")) {
            Msg msg1 = new Msg("I miss you!", Msg.RECEIVED, R.drawable.cai);
            msgList.add(msg1);
            Msg msg2 = new Msg("I miss you,too!", Msg.SENT, R.drawable.person);
            msgList.add(msg2);
            Msg msg3 = new Msg("I will come back soon!", Msg.RECEIVED, R.drawable.cai);
            msgList.add(msg3);
        }
        if (id.equals("1")) {
            Msg msg1 = new Msg("I miss you!", Msg.RECEIVED, R.drawable.he);
            msgList.add(msg1);
            Msg msg2 = new Msg("I miss you,too!", Msg.SENT, R.drawable.person);
            msgList.add(msg2);
            Msg msg3 = new Msg("I will come back soon!", Msg.RECEIVED, R.drawable.he);
            msgList.add(msg3);
        }
        if (id.equals("2")) {
            Msg msg1 = new Msg("I miss you!", Msg.RECEIVED, R.drawable.lv);
            msgList.add(msg1);
            Msg msg2 = new Msg("I miss you,too!", Msg.SENT, R.drawable.person);
            msgList.add(msg2);
            Msg msg3 = new Msg("I will come back soon!", Msg.RECEIVED, R.drawable.lv);
            msgList.add(msg3);
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //    Inflate the menu; this adds items to the action bar if it is present.
        /*  getMenuInflater().inflate(R.menu.main, menu);*/
        return true;
    }
}
